home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PRUS101.ZIP / FDPMI.PAS < prev    next >
Pascal/Delphi Source File  |  1994-07-20  |  5KB  |  135 lines

  1. Unit FDPMI; { routines for DOS/DPMI communication }
  2.  (***************************************************************************
  3.  
  4.            RELEASE 1.00 - as contained in the file PRUS100.LZH
  5.                  by Max Maischein, 2:244/1106.17, GERMANY
  6.  
  7.                --------------------------------------------
  8.                 organized for Fido's PASCAL related echoes
  9.                --------------------------------------------
  10.  
  11.      06/15/1994 to --/--/---- by Max Maischein, 2:244/1106.17, GERMANY
  12.  
  13.            As far as third party copyrights are not violated this
  14.            source code is hereby placed to the public domain. Use
  15.            it whatever way you want, but use AT YOUR OWN RISK.
  16.  
  17.            In case you should modify the source rather send your
  18.            modifications to the unit's current organizer (see above for
  19.            NM address) than to spread it on your own. This will help to
  20.            keep the unit updated and grant a certain standard to all
  21.            other users as well.
  22.  
  23.            The unit is currently still under work. So it might greatly
  24.            benefit of your participation.
  25.  
  26.            Those who contributed to the following piece of source,
  27.            listed in alphabethical order:
  28.         ================================================================
  29.            Jochen Magnus (SEGDEMO.PAS), Max Maischein (collecting,
  30.            documenting, testing etc.), Raphael Vanney (program CHARSET
  31.            .PAS)
  32.         ================================================================
  33.            YOUR NAME WILL APPEAR HERE IF YOU CONTRIBUTE USEFUL SOURCE.
  34.  
  35.  ***************************************************************************)
  36.  
  37. {$I FDEFINE.DEF}
  38.  
  39. Interface
  40.  
  41. Type TRealModeRegs  =
  42.      Record
  43.           Case Integer Of
  44.           0: ( EDI, ESI, EBP, EXX, EBX, EDX, ECX, EAX: Longint;
  45.                Flags, ES, DS, FS, GS, IP, CS, SP, SS: Word) ;
  46.           1: ( DI,DIH, SI, SIH, BP, BPH, XX, XXH: Word;
  47.                Case Integer of
  48.                  0: (BX, BXH, DX, DXH, CX, CXH, AX, AXH: Word);
  49.                  1: (BL, BH, BLH, BHH, DL, DH, DLH, DHH,
  50.                      CL, CH, CLH, CHH, AL, AH, ALH, AHH: Byte));
  51.      End;
  52. (* Use these and RealModeInt() instead of Registers and Intr() under DPMI *)
  53.  
  54. Type TLongRec = Record
  55.        Case Byte of
  56.          0 : ( L : LongInt );
  57.          1 : ( wLo : Word;
  58.                wHi : Word; );
  59.          2 : ( iLo : Integer;
  60.                iHi : Integer );
  61.      End;
  62.  
  63. Function RealModeInt( IntNo : Byte; Var RealRegs : TRealModeRegs) : Boolean;
  64. (* Replacement for Intr(), returns False on error *)
  65.  
  66. Function NewSelector( Base, Limit : Longint) : Word;
  67. (* Returns a new selector for the range [Base - (Base+Limit)]        *)
  68. (* Base is the linear address of the memory location you want to use *)
  69. (* You must free this selector after use with FreeSelector()         *)
  70.  
  71. Function AllocateLowMem( Size : Word; var PMPointer : Pointer ) : Word;
  72. (* Allocates some memory in the first MB *)
  73. (* Returns a pointer to it and a segment to pass to the real mode routine *)
  74.  
  75. Procedure FreeLowMem(Var PMPointer : Pointer );
  76. (* Frees memory allocated with AllocateLowMem() *)
  77.  
  78. Implementation
  79. Uses WinAPI;
  80.  
  81. Function RealModeInt( IntNo : Byte; Var RealRegs : TRealModeRegs) : Boolean;
  82. Assembler;
  83. { This function switches to real mode and issues the specified interrupt,
  84.   after filling the registers with values stored in RealRegs.
  85.   If SS/SP as specified in RealRegs are Nil, the DPMI server provides a
  86.   small stack. For more discution of this, see Ralf Brown's INTERxx.ZIP }
  87. Asm
  88.                         mov     ax, 0300h       { DPMI function "simulate real
  89. mode int" }
  90.                         mov     bl, [IntNo]
  91.                         xor     bh, bh          { 0 as requested by DPMI v1.0 }
  92.                         xor     cx, cx          { bytes to copy onto "remote"
  93. stack }
  94.                         les     di, [RealRegs]
  95.                         int     31h             { Returns CF set on error }
  96.                         mov     ax, 1           { assume everything went OK }
  97.                         sbb     ax, 0           { if carry set, decrement ax }
  98.                                                 { to signal an error }
  99. End;
  100.  
  101. Function NewSelector(Base,Limit:longint) : Word;
  102. Var Sel : Word;
  103. Begin
  104.   Sel := AllocSelector(0);
  105.   If (sel<>0) and (setSelectorBase(sel,base)=sel) and (setSelectorLimit(sel,
  106. limit)=0)
  107.     then newSelector:=sel
  108.     else newSelector:=0;
  109. End;
  110.  
  111. Function AllocateLowMem( Size : Word; var PMPointer : Pointer ) : Word;
  112. { This procedure allocates memory in the first megabyte, and returns two
  113.   ways of accessing it : a pointer valid in protected mode, and the
  114.   segment part of a pointer valid in real mode ; offset part is always 0 }
  115.  
  116. Var  Adr  : LongInt ;
  117. Begin
  118.   Adr:=GlobalDOSAlloc(Size) ;
  119.   If Adr=0 then RunError( 0 );
  120.   PMPointer := Ptr( TLongRec(Adr).wLo, 0);
  121.   AllocateLowMem := TLongRec(Adr).wHi;
  122. End ;
  123.  
  124. Procedure FreeLowMem(Var PMPointer : Pointer ) ;
  125. { Frees memory allocated with AllocateLowMem }
  126. Begin
  127.   GlobalDOSFree(Seg( PMPointer^ ));
  128.   PMPointer := nil;
  129. End ;
  130. {$IFnDEF DPMI}
  131. {$IFnDEF Windows}
  132.   You are compiling WHAT ??
  133. {$ENDIF}
  134. {$ENDIF}
  135. End.